﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace UIT_CV_Management
{
    public class User
    {
        public String ID;
        public String Username;
        public String Password;
        public String Email;
        public Boolean InfoSharing;
        public String MaLyLichKhoaHoc;
        public LyLichKhoaHoc LyLichKhoaHoc
        {
            get
            {
                return new LyLichKhoaHoc(MaLyLichKhoaHoc);
            }
        }

        public User(String _Username)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM Users WHERE Username=N'" + _Username + "'");
            if (result.ReturnCode == 200)
            {
                DataRow row = result.Table.Rows[0];
                ID = row["ID"].ToString();
                Username = row["Username"].ToString();
                Password = row["Password"].ToString();
                Email = row["Email"].ToString();
                InfoSharing = row.Field<Boolean>("InfoSharing");
                MaLyLichKhoaHoc = row["MaLyLichKhoaHoc"].ToString();
            }
        }

        public User(String _Username, String _Password, String _Email, Boolean _InfoSharing)
        {
            ID = Database.getID("Users", "ID");
            Username = _Username;
            Password = _Password;
            Email = _Email;
            InfoSharing = _InfoSharing;
        }
        
        public bool Login(String _Password)
        {
            if (this.Password == _Password)
                return true;
            return false;
        }

        public Boolean Save()
        {
            QueryResult result;
            if (Database.isDupplicate("Users", "ID", ID))
            {
                //Update
                result = Database.shareDatabase.Query("UPDATE Users SET " +
                    "Username = N'" + Username + "', " +
                    "Password = N'" + Password + "', " +
                    "Email = '" + Email + "' " +
                    "WHERE ID = '" + ID + "'");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
            else
            {
                //Insert
                result = Database.shareDatabase.Query("INSERT INTO Users VALUES(" +
                    "'" + ID + "', " +
                    "N'" + Username + "', " +
                    "N'" + Password + "', " +
                    "'" + Email + "', 1, NULL)");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
        }

    }
}
